package com.isczy.acl.controller;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.acl.entity.Role;
import com.isczy.acl.service.RoleService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /***
     * ***************************************
     * 功能描述:获取角色分页列表
     * @param page
     * @param limit
     * @param role
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:00】
     * @author modify:
     */
    @ApiOperation(value = "获取角色分页列表")
    @GetMapping("{page}/{limit}")
    public DataResult index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            Role role) {

        try{
            Page<Role> pageParam = new Page<>(page, limit);
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(role.getRoleName())) {
                wrapper.like("role_name",role.getRoleName());
            }
            roleService.page(pageParam,wrapper);
            return DataResult.builder()
                    .data("items", pageParam.getRecords())
                    .data("total", pageParam.getTotal())
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("获取角色分页列表失败")
                    .params(role)
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:根据获取角色
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:00】
     * @author modify:
     */
    @ApiOperation(value = "获取角色")
    @GetMapping("get/{id}")
    public DataResult get(@PathVariable String id) {
        try{
            Role role = roleService.getById(id);
            return DataResult.builder()
                    .data("item", role)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据获取角色失败")
                    .params(id)
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:新增角色
     * @param role
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:04】
     * @author modify:
     */
    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public DataResult save(@RequestBody Role role) {
        try{
            roleService.save(role);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("新增角色失败")
                    .params(role)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改角色
     * @param role
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:04】
     * @author modify:
     */
    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public DataResult updateById(@RequestBody Role role) {
        try{
            roleService.updateById(role);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("修改角色失败")
                    .params(role)
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:删除角色
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:05】
     * @author modify:
     */
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public DataResult remove(@PathVariable String id) {
        try{
            roleService.removeById(id);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除角色失败")
                    .params(id)
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:根据id列表删除角色
     * @param idList
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:06】
     * @author modify:
     */
    @ApiOperation(value = "根据id列表删除角色")
    @DeleteMapping("batchRemove")
    public DataResult batchRemove(@RequestBody List<String> idList) {
        try{
            roleService.removeByIds(idList);
            return DataResult.builder().successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据id列表删除角色失败")
                    .params(ArrayUtil.toString(idList))
                    .build();
        }

    }
}

